package sd.qd.xiaoming.coffeedesk.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sd.qd.xiaoming.coffeedesk.dao.CdDirectoryDAO;
import sd.qd.xiaoming.coffeedesk.entity.CdDirectory;

@Transactional
@Service
public class CdDirectoryService extends BaseService<CdDirectoryDAO, CdDirectory> {

	@Autowired
	private CdDirectoryDAO dao;

    public boolean deleteById(final Long id) {
        try {
            this.dao.delete(this.dao.findById(id));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<CdDirectory> findByPid(final Long pid) {
        CdDirectory parent = null;
        if (null != pid) {
            parent = this.dao.findById(pid);
        }
        return this.dao.findByParent(parent);
    }

    public boolean isDirectoryExists(final Long pid, final String name) {
        for (CdDirectory d : this.dao.findById(pid).getChildren()) {
            if (d.getName().equals(name)) {
                return true;
            }
        }
        return false;
    }

}
